var app=getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        depositprictrueList:[],
        issuccessMask:false,
        isbalanceMask:false
    },
// 上传铺贴定金图
    bindUnplone() {
      let t = this
      wx.chooseMedia({
        count: 9-t.data.depositprictrueList.length,
        mediaType: ['image'],
        maxDuration: 60,
        success(res) {      
          let depositprictrueList= t.data.depositprictrueList
          
          res.tempFiles.forEach(item=> depositprictrueList.push(item.tempFilePath))
          t.setData({
            depositprictrueList:depositprictrueList
          })
        
        }
      })
    },
    // 预览铺贴定金图
    previewImage(e){
      let depositprictrueList=this.data.depositprictrueList
      let url=e.currentTarget.dataset.url
        wx.previewImage({
          current: url, // 当前显示图片的 http 链接
          urls:depositprictrueList , // 需要预览的图片 http 链接列表
          success() {
            
          }
        })
      },
      // 删除铺贴定金图
      del(e){
        let depositprictrueList=this.data.depositprictrueList
        depositprictrueList.splice(e.currentTarget.dataset.index,1)
        this.setData({
            depositprictrueList:depositprictrueList
        })
      },
    async tab(){
      if (this.data.depositprictrueList.length== 0) {
        wx.showToast({
          title: '请上传客户签字验收表',
          duration: 2500, //持续的时间
            icon: 'none', //图标
        })
        return
      }

      this.setData({
        isbalanceMask:true
      })


     
    },
    async no(){

       let depositprictrueList=await this.upload(this.data.depositprictrueList)
      this.setData({
        depositprictrueList:depositprictrueList
      })
      this.saveCustomerCheck()
    },

    yes(){
      wx.navigateTo({
        url: '/shopowner/pages/collections/collections?todo=finished&orderId='+this.data.orderId+'&&projectOrder='+JSON.stringify(this.data.projectOrder)+"&&depositprictrueList="+JSON.stringify(this.data.depositprictrueList),
      })
    },

    saveCustomerCheck(){
      let t = this
      wx.showLoading({
        title: '加载中。。。', //提示的内容
        mask: true //显示透明蒙层 防止触摸穿透
      })
      wx.request({
        url: app.globalData.Interfaceurl + 'saveCustomerCheck',
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
        },
        data: {
          userId: app.globalData.userId,
          token: t.data.token,
          orderId:t.data.orderId,
          customerCheckProof:t.data.depositprictrueList.join(","),

        },
        success: res => {
          wx.hideLoading();
          if (res.data.result != -1) {
            this.setData({
              issuccessMask: true,
            })
          } else {
            wx.showToast({
              title: res.data.message,
              duration: 2500, //持续的时间
              icon: 'none', //图标
            })
           t.setData({
          token:  t.token()
           })
          }
  
  
  
  
        },
        fail: err => {
          wx.showToast({
            title: '请求失败',
            duration: 2500, //持续的时间
            icon: 'none', //图标
          })
        }
      })
    },
    hidesuccessMask(){
      this.setData({
        issuccessMask:false
      })
      let pages = getCurrentPages();
      let prevPage = pages[pages.length - 3]; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。\\
      prevPage.getOrderDetails()
      wx.setStorageSync('istab', true)

      prevPage.setData({
        todo: ''
      })

      wx.navigateBack({
        delta: 2,
      })
  
    },
       // 上传附件
       upload(arr) {
        return new Promise((reslove, reject) => {
          if (arr.length == 0) {
            reslove([])
            return
          }
          wx.showLoading({
            title: '上传中...', //提示的内容
            mask: true //显示透明蒙层 防止触摸穿透
          })
          let finishFilesList = []
          arr.forEach((item, index) => {
            wx.uploadFile({
              url: app.globalData.Interfaceurl + 'saveFileUpload',
              filePath: item,
              name: 'file',
              formData: {
                userId: app.globalData.userId,
    
              },
              header: {
                "content-type": "multipart/form-data"
              },
              success: function (res) {
                finishFilesList.push(JSON.parse(res.data).fileUrl)
                if (index >= arr.length - 1) {
                  setTimeout(()=>{
                    wx.hideLoading();
                    reslove(finishFilesList)
                   },500)
                }
              },
              fail: err => {
                return err
              }
            })
          })
        })
      },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

      if(options.orderId){
        this.setData({
          orderId:options.orderId,
          token:app.token(),
          projectOrder:JSON.parse(options.projectOrder)
          })
      }
      
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})